<%@page contentType="text/html" import="java.sql.*,java.text.*,java.util.*,merlin.beans.*,merlin.dao.*,merlin.managers.*,merlin.utils.*" %>

<!--
	Name		: Merlin E-Book Report Download Page.
	Developed By: Sandip Bhattacharya (sandipkb@yahoo.com)
	Version 	: 1.0
-->

<%
	String token = request.getParameter("token");
	
	int orderId = -1;
	
	try{
		orderId = Integer.parseInt(token,16);
	}catch(Exception e){
		System.out.println(e);
	}
	
	System.out.println("Order ID " + orderId);
	
	// Check whether the request is coming from a link generated from the BIB service, or from a normal Merlin reports purchase invoice.
	// The BIB service generates a differently formatted PDF.
	boolean bibService = false;
	String service = request.getParameter("service");
	if("bib".equalsIgnoreCase(service)){
		bibService = true;
	}
	System.out.println("bib service " + bibService);

	boolean isValidOrder = IndustryWatchManager.isValidEBooksDownloadOrder(orderId);

%>
<HTML>
	<HEAD>
	    <TITLE>Download E-Books</TITLE>

		<!-- begin imports for sorting of the table -->
        <script type="text/javascript" src="mr_scripts/sorttable.js"></script>
        <style>
			.sortablecolumn {
				cursor: pointer;
				cursor: hand;
			}
        </style>
		<!-- ends imports for sorting of the table -->

		<script>
	    	function downloadReport(uuid, reportFormat){
	    		document.location = "merlin?mode=pdfReportDownload&downloadId=" + uuid + "&bibService=" + <%=bibService%> + "&reportFormat=" + reportFormat;
	    	}
	    </script>

	    <style type="text/css">
	    	th{
	    		border-style: none none solid none;
				border-width:1px;
	    	}
	    </style>
		<link href="css/master.css" rel="stylesheet" type="text/css">

		<!-- refresh the page every 10 mins to see if new reports have been generated -->
		<meta http-equiv="refresh" content="600">

	</HEAD>
	<BODY>
	    <jsp:include page="mr_header.jsp"/>

	    <p/>
		<%
			if(isValidOrder){
		%>
		<table width="750" align="center">
			<%
				// all customers will at least have PDF downloads
				PDFDownloadDetailsBean[] arrDownloads = IndustryWatchManager.getPDFDownloadDetailsByOrderId(orderId, bibService, MerlinUtilities.REPORT_TYPE_PDF);
				if(arrDownloads != null && arrDownloads.length > 0){
			%>
			<tr>
				<td>
					<p/>&nbsp;<p/>
					<b style="font:bold 11px verdana,arial, sans-serif;">eBook Report - PDF format</b>
					<table class="sortable" width='100%' cellspacing='0' cellpadding = '5' style="border-style: solid;border-width: 1px;font:11px verdana,arial, sans-serif ;">
						<thead>
							<tr>
								<th class="sortablecolumn">Report Title</th>
								<th style="text-align:right" class="sorttable_nosort">Size</th>
								<th class="sortablecolumn">Edition</th>
								<th class="sorttable_nosort">Status</th>
							</tr>
						</thead>
						<tbody>
						<%
							SimpleDateFormat dfmt = new SimpleDateFormat("dd/MM/yy");

							for(int i=0;i<arrDownloads.length;i++){

								int status = arrDownloads[i].getStatus();
								String message = "";
								boolean clickable = false;
								String rowColor = null;
								String cursor = "default";

								switch(status){
									case MerlinUtilities.REPORT_STATUS_IN_PROGRESS:
										message = "Preparing Edition";
										rowColor = "black";
										break;
									case MerlinUtilities.REPORT_STATUS_AVAILABLE:
										clickable = true;
										message = "Click to download";
										rowColor = "#038243";
										cursor = "pointer";
										break;
									case MerlinUtilities.REPORT_STATUS_ALREADY_DOWNLOADED:
										message = "Downloaded on " + dfmt.format(arrDownloads[i].getDownloadDate());
										rowColor = "red";
										break;
								}

								out.print("<tr style='color:" + rowColor + ";cursor:" + cursor + "'" + (clickable?"onclick='downloadReport(\"" + arrDownloads[i].getDownloadUuid() + "\"," + MerlinUtilities.REPORT_TYPE_PDF + ")'":"") + ">");
								out.print("<td>" + arrDownloads[i].getReportTitle() + "</td>");
								out.print("<td style='text-align: right'>" + MerlinUtilities.formatFileSize(arrDownloads[i].getReportSize()) + "</td>");
								out.print("<td>" + arrDownloads[i].getReportEdition() + "</td>");
								out.print("<td>" + message + "</td>");
								out.println("</tr>");

							}
						%>
						</tbody>
					</table>
				</td>
			</tr>
			<%
				}else{
			%>
				<tr><td><p>No e-books to download</td></tr>
			<%
				}
			%>
			
			<%
				// Check if the usre is allowed to download Spreadsheet and Rankings data or not. 
				CustomerDetailsBean customerBean = IndustryWatchManager.getCustomerDetailsByEbookDownloadOrderId(orderId);
			%>
			<%
				// Spreadsheet.
				if(customerBean != null && customerBean.getSpreadsheetData() != null){
					// fetch spreadsheet data
					arrDownloads = IndustryWatchManager.getPDFDownloadDetailsByOrderId(orderId, bibService, MerlinUtilities.REPORT_TYPE_XLS);
					if(arrDownloads != null && arrDownloads.length > 0){
			%>
				<tr>
					<td>
						<p/>&nbsp;<p/>
						<b style="font:bold 11px verdana,arial, sans-serif;">Report data - spreadsheet format</b>
						<table class="sortable" width='100%' cellspacing='0' cellpadding = '5' style="border-style: solid;border-width: 1px;font:11px verdana,arial, sans-serif ;">
							<thead>
								<tr>
									<th class="sortablecolumn">Report Title</th>
									<th style="text-align:right" class="sorttable_nosort">Size</th>
									<th class="sortablecolumn">Edition</th>
									<th class="sorttable_nosort">Status</th>
								</tr>
							</thead>
							<tbody>
							<%
								SimpleDateFormat dfmt = new SimpleDateFormat("dd/MM/yy");
	
								for(int i=0;i<arrDownloads.length;i++){
	
									int status = arrDownloads[i].getStatus();
									String message = "";
									boolean clickable = false;
									String rowColor = null;
									String cursor = "default";
	
									switch(status){
										case MerlinUtilities.REPORT_STATUS_IN_PROGRESS:
											message = "Preparing Edition";
											rowColor = "black";
											break;
										case MerlinUtilities.REPORT_STATUS_AVAILABLE:
											clickable = true;
											message = "Click to download";
											rowColor = "#038243";
											cursor = "pointer";
											break;
										case MerlinUtilities.REPORT_STATUS_ALREADY_DOWNLOADED:
											message = "Downloaded on " + dfmt.format(arrDownloads[i].getDownloadDate());
											rowColor = "red";
											break;
									}
	
									out.print("<tr style='color:" + rowColor + ";cursor:" + cursor + "'" + (clickable?"onclick='downloadReport(\"" + arrDownloads[i].getDownloadUuid() + "\"," + MerlinUtilities.REPORT_TYPE_XLS + ")'":"") + ">");
									out.print("<td>" + arrDownloads[i].getReportTitle() + "</td>");
									out.print("<td style='text-align: right'>" + MerlinUtilities.formatFileSize(arrDownloads[i].getReportSize()) + "</td>");
									out.print("<td>" + arrDownloads[i].getReportEdition() + "</td>");
									out.print("<td>" + message + "</td>");
									out.println("</tr>");
	
								}
							%>
							</tbody>
						</table>
					</td>
				</tr>
			
			<%
					}
				}
			%>
			
			<%
				// Rankings.
				if(customerBean != null && customerBean.getRankingsData() != null){
					// fetch rankings data
					arrDownloads = IndustryWatchManager.getPDFDownloadDetailsByOrderId(orderId, bibService, MerlinUtilities.REPORT_TYPE_RANKINGS);
					if(arrDownloads != null && arrDownloads.length > 0){
			%>
				<tr>
					<td>
						<p/>&nbsp;<p/>
						<b style="font:bold 11px verdana,arial, sans-serif;">Report rankings - spreadsheet format</b>						 
						<table class="sortable" width='100%' cellspacing='0' cellpadding = '5' style="border-style: solid;border-width: 1px;font:11px verdana,arial, sans-serif ;">
							<thead>
								<tr>
									<th class="sortablecolumn">Report Title</th>
									<th style="text-align:right" class="sorttable_nosort">Size</th>
									<th class="sortablecolumn">Edition</th>
									<th class="sorttable_nosort">Status</th>
								</tr>
							</thead>
							<tbody>
							<%
								SimpleDateFormat dfmt = new SimpleDateFormat("dd/MM/yy");
	
								for(int i=0;i<arrDownloads.length;i++){
	
									int status = arrDownloads[i].getStatus();
									String message = "";
									boolean clickable = false;
									String rowColor = null;
									String cursor = "default";
	
									switch(status){
										case MerlinUtilities.REPORT_STATUS_IN_PROGRESS:
											message = "Preparing Edition";
											rowColor = "black";
											break;
										case MerlinUtilities.REPORT_STATUS_AVAILABLE:
											clickable = true;
											message = "Click to download";
											rowColor = "#038243";
											cursor = "pointer";
											break;
										case MerlinUtilities.REPORT_STATUS_ALREADY_DOWNLOADED:
											message = "Downloaded on " + dfmt.format(arrDownloads[i].getDownloadDate());
											rowColor = "red";
											break;
									}
	
									out.print("<tr style='color:" + rowColor + ";cursor:" + cursor + "'" + (clickable?"onclick='downloadReport(\"" + arrDownloads[i].getDownloadUuid() + "\"," + MerlinUtilities.REPORT_TYPE_RANKINGS + ")'":"") + ">");
									out.print("<td>" + arrDownloads[i].getReportTitle() + "</td>");
									out.print("<td style='text-align: right'>" + MerlinUtilities.formatFileSize(arrDownloads[i].getReportSize()) + "</td>");
									out.print("<td>" + arrDownloads[i].getReportEdition() + "</td>");
									out.print("<td>" + message + "</td>");
									out.println("</tr>");
	
								}
							%>
							</tbody>
						</table>
					</td>
				</tr>
			
			<%
					}
				}
			%>
			
		</table>
		<br>Note that files may take some minutes to successfully download.
		<%
			}else{
		%>
			Order is invalid or its validity has expired.
		<%
			}
		%>
	</BODY>
</HTML>